package com.whf.demo.controller;

import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

@RestController
public class SimpleController {
    @GetMapping("/whf")
    public String hello() {
        return "<!DOCTYPE html>"
                + "<html lang=\"zh\">"
                + "<head>"
                + "<meta charset=\"UTF-8\">"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
                + "<title>欢迎页面</title>"
                + "<style>"
                + "body {"
                + "    font-family: Arial, sans-serif;"
                + "    display: flex;"
                + "    justify-content: center;"
                + "    align-items: center;"
                + "    height: 100vh;"
                + "    margin: 0;"
                + "    text-align: center;"
                + "}"
                + ".container {"
                + "    text-align: center;"
                + "    border: 2px solid #000;"
                + "    padding: 20px;"
                + "    border-radius: 10px;"
                + "}"
                + "</style>"
                + "</head>"
                + "<body>"
                + "<div class=\"container\">"
                + "<h1>欢迎来到我的简单 Spring Boot 项目！</h1>"
                + "<p>作者：王瀚锋111</p>"
                + "</div>"
                + "</body>"
                + "</html>";
    }
    @GetMapping("/image")
    public ResponseEntity<byte[]> getImage() throws IOException {
        // 假设图片是 PNG，位于 resources/static/images/example.png
        ClassPathResource imgFile = new ClassPathResource("images/a.png");

        byte[] bytes = Files.readAllBytes(Path.of(imgFile.getURI()));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG); // 设置为 PNG 格式

        return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
    }
}
